package com.chat.datea.adapter;

import android.content.Context;
import android.support.v7.widget.PopupMenu;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chat.datea.R;
import com.chat.datea.bean.greendao.Collect;
import com.chat.datea.bean.greendao.CollectDao;
import com.chat.datea.bean.local_music.LocalMusicEntity;
import com.chat.datea.utils.daosingleton.DaoSingleton;

import java.util.ArrayList;

import de.greenrobot.dao.query.QueryBuilder;

/**
 * Created by YanBinXie on 15/10/31.
 */
public class Local_Adapter extends BaseAdapter {
    private Context context;
    private ArrayList<LocalMusicEntity> musicEntities;

    private Collect  collect;
    private CollectDao collectDao;

    public Local_Adapter(ArrayList<LocalMusicEntity> musicEntities, Context context) {
        this.musicEntities = musicEntities;
        this.context = context;
        collectDao = DaoSingleton.getInstance().getCollectDao();
        collect = new Collect();
    }


    @Override
    public int getCount() {
        return musicEntities != null && musicEntities.size() > 0 ? musicEntities.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return musicEntities != null && musicEntities.size() > 0 ? musicEntities.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return musicEntities != null && musicEntities.size() > 0 ? position : 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.local_list_item, null);
            holder.music_name = (TextView) convertView.findViewById(R.id.music_name);
            holder.music_singer = (TextView) convertView.findViewById(R.id.music_singer);
            holder.music_album = (TextView) convertView.findViewById(R.id.music_album);
            holder.more = (ImageView) convertView.findViewById(R.id.more);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        try {
            holder.music_name.setText(musicEntities.get(position).getTitle());
        } catch (Exception e) {

        }
        try {
            holder.music_album.setText(musicEntities.get(position).getAlbum());
        } catch (Exception e) {

        }
        try {
            holder.music_singer.setText(musicEntities.get(position).getArtist());
        } catch (Exception e) {

        }
        holder.more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu popupMenu = new PopupMenu(context,holder.more);
                popupMenu.getMenuInflater().inflate(R.menu.music_menu, popupMenu.getMenu());
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        Log.e("MusicRecomFragment_List", "您点击了收藏");
                        QueryBuilder qb = collectDao.queryBuilder();
                        qb.where(CollectDao.Properties.Name.eq(musicEntities.get(position).getTitle()));
                        qb.where(CollectDao.Properties.From.eq("本地"));
                        ArrayList<Collect> collects = (ArrayList<Collect>) qb.list();
                        if (collects.size()>0) {
                            Toast.makeText(context, "您已收藏", Toast.LENGTH_SHORT).show();
                        }else{
                            collect.setFrom("本地");
                            collect.setName(musicEntities.get(position).getTitle());
                            collect.setSongId("");
                            collect.setSinger(musicEntities.get(position).getAlbum());
                            collect.setAlbumId(musicEntities.get(position).getAlbum_id());
                            collect.setId(musicEntities.get(position).getId());
                            collect.setPosition(1);
                            collect.setUrl(musicEntities.get(position).getUrl());
                            collect.setWant("收藏");
                            collectDao.insert(collect);
                            Toast.makeText(context, "收藏成功", Toast.LENGTH_SHORT).show();
                        }

                        return true;
                    }
                });
                popupMenu.show();
                Log.e("MusicRecomFragment_List", "点击了更多");

            }
        });


        return convertView;
    }

    class ViewHolder {
        TextView music_name, music_singer, music_album;
        ImageView more;

    }
}
